package com.gdiss.www.auto;


import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

/**
 * 代码生成器
 * @author yly
 * @date 2021/2/2 19:11
 */
public class AutoCode {


    private static final String database = "db_gd_industrial_software_association";
    private static final String username = "root";
    private static final String password = "123456";


    public static void main(String[] args) {
        //需要构建一个 代码生成器 对象
        AutoGenerator mpg = new AutoGenerator();
        //配置策略

        //1.全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");//项目路径
        gc.setOutputDir(projectPath + "/src/main/java");//代码生成路径
        gc.setAuthor("yly");//设置作者
        gc.setOpen(false);//取消生成后打开的操作
        gc.setFileOverride(false);//取消文件覆盖
        gc.setServiceName("%sService");//去I前缀
        gc.setIdType(IdType.AUTO);//设置主键填充策略
        gc.setDateType(DateType.ONLY_DATE);//设置时间填充策略
        gc.setSwagger2(false);//swagger

        mpg.setGlobalConfig(gc);

        //2.配置数据源
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUrl("jdbc:mysql://localhost:3306/"+database+"?serverTimezone=UTC");
        dsc.setUsername(username);
        dsc.setPassword(password);
        dsc.setDbType(DbType.MYSQL);

        mpg.setDataSource(dsc);

        //3.包的配置
        PackageConfig pc = new PackageConfig();
        //pc.setModuleName("blog");
        pc.setParent("com.gdiss.www");
        pc.setEntity("entity");
        pc.setMapper("mapper");
        pc.setService("service");
        pc.setController("controller");

        mpg.setPackageInfo(pc);

        //4.策略配置
        StrategyConfig sc = new StrategyConfig();
        sc.setInclude(
                "content_state",
                "content_type"
        );//包含需要映射的表名

        sc.setNaming(NamingStrategy.underline_to_camel);
        sc.setColumnNaming(NamingStrategy.underline_to_camel);
        //sc.setSuperEntityClass("你自己的父类实体,没有就不用设置!");
        sc.setEntityLombokModel(true);
        sc.setRestControllerStyle(true);
        //sc.setControllerMappingHyphenStyle(true);//开启路径下划线命名
        //sc.setControllerMappingHyphenStyle(false);
        sc.setLogicDeleteFieldName("del");//设置逻辑删除
        //自动填充配置
        //TableFill creatTime = new TableFill("creat_time", FieldFill.INSERT);
        //TableFill updateTime = new TableFill("update_time", FieldFill.INSERT_UPDATE);
        //sc.setTableFillList(Arrays.asList(creatTime,updateTime));
        //乐观锁
        //sc.setVersionFieldName("version");

        mpg.setStrategy(sc);

        //执行
        mpg.execute();
    }
}
